package com.zlx.sharelive.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.zlx.module_base.base_api.res_data.DynamicListBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_fg.BaseFg;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_base.base_util.TimeUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.activity.CommunityDetailsAc;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.StringUt;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的上传——文章
 */
public class MyArticleFg2 extends BaseFg {

    @BindView(R.id.sw_layout)
    SwipeRefreshLayout sw_layout;
    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private String checkStatus = "1";
    private int page = 1;
    private List<DynamicListBean.InfoDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<DynamicListBean.InfoDTO> mAdapter;
    private List<String> mImageList = new ArrayList<>();
    private CommonRecyclerViewAdapter<String> mImageAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fg_my_article2;
    }

    @Override
    protected void initViews() {
        super.initViews();

        getDynamicList(page);
        initRecyclerView();

        sw_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getDynamicList(page);
            }
        });
    }


    @Override
    public void onStart() {
        super.onStart();
        page = 1;
        getDynamicList(page);
    }


    private void getDynamicList(int page) {
        ApiUtil.getServiceApi().getDynamicList(SPUtil.getToken(getContext()),
                "",
                "0",
                "",
                "",
                "",
                String.valueOf(page),
                "",
                "1",
                "",
                "",
                "").observe(getViewLifecycleOwner(), new BaseObserver<>(new BaseObserverCallBack<ApiResponse<DynamicListBean>>() {
            @Override
            public void onSuccess(ApiResponse<DynamicListBean> data) {
                sw_layout.setRefreshing(false);
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                if (checkStatus.equals("0")) {
                                    for (int i = 0; i < data.getData().getInfo().size(); i++) {
                                        if (data.getData().getInfo().get(i).getCheckStatus().equals("0")) {
                                            mList.add(data.getData().getInfo().get(i));
                                        }
                                    }
                                } else if (checkStatus.equals("1")) {
                                    for (int i = 0; i < data.getData().getInfo().size(); i++) {
                                        if (data.getData().getInfo().get(i).getCheckStatus().equals("1")) {
                                            mList.add(data.getData().getInfo().get(i));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }));
    }


    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(getContext()));

        mAdapter = new CommonRecyclerViewAdapter<DynamicListBean.InfoDTO>(getContext(), mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, DynamicListBean.InfoDTO entity, int position) {
                ImageView iv_community_head = holder.getView(R.id.iv_community_head);

                GlideImgManager.glideLoader(getContext(), entity.getAvatar(), iv_community_head, 0);

                holder.setText(R.id.tv_community_nickname, entity.getNickname());

                ImageView tv_yin = holder.getView(R.id.tv_yin);//阴影 判断是否是收费的

                if (!StringUt.isEmpty(entity.getIzFee())) {
                    if (entity.getIzFee().equals("1")) {
                        tv_yin.setVisibility(View.VISIBLE);
                    } else {
                        tv_yin.setVisibility(View.GONE);
                    }
                } else {
                    tv_yin.setVisibility(View.GONE);
                }

                ImageView iv_community_vip = holder.getView(R.id.iv_community_vip);
                ImageView iv_community_goddess = holder.getView(R.id.iv_community_goddess);

                holder.setText(R.id.tv_reward_number, entity.getReword());

                if (entity.getIsDz().equals("1")) {
                    ((ImageView) holder.getView(R.id.iv_fabulous_img)).setImageResource(R.mipmap.fabulous_img_true);
                } else {
                    ((ImageView) holder.getView(R.id.iv_fabulous_img)).setImageResource(R.mipmap.fabulous_img2);
                }

                try {
                    holder.setText(R.id.tv_community_time, TimeUtil.getTimeStringAutoShort2(TimeUtil.ConverToDate(entity.getDatetime()), true));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                holder.setText(R.id.tv_community_address, entity.getCity());
                holder.setText(R.id.tv_community_text_title, entity.getTitle());
                holder.setText(R.id.tv_community_text_content, entity.getText());
                holder.setText(R.id.tv_share_number, entity.getShares());
                holder.setText(R.id.tv_fabulous_number, entity.getLikes());
                holder.setText(R.id.tv_comment_number, entity.getComments());

//                holder.setText(R.id.tv_community_time, entity.getDatetime());

                mImageList.clear();
                if (!StringUt.isEmpty(entity.getThumbs())) {
                    mImageList.add(entity.getThumbs());
                }
                if (!StringUt.isEmpty(entity.getThumb2())) {
                    mImageList.add(entity.getThumb2());
                }
                if (!StringUt.isEmpty(entity.getThumb3())) {
                    mImageList.add(entity.getThumb3());
                }
                if (!StringUt.isEmpty(entity.getThumb4())) {
                    mImageList.add(entity.getThumb4());
                }
                if (!StringUt.isEmpty(entity.getThumb5())) {
                    mImageList.add(entity.getThumb5());
                }
                if (!StringUt.isEmpty(entity.getThumb6())) {
                    mImageList.add(entity.getThumb6());
                }
                if (!StringUt.isEmpty(entity.getThumb7())) {
                    mImageList.add(entity.getThumb7());
                }
                if (!StringUt.isEmpty(entity.getThumb8())) {
                    mImageList.add(entity.getThumb8());
                }
                if (!StringUt.isEmpty(entity.getThumb9())) {
                    mImageList.add(entity.getThumb9());
                }

                RecyclerView rv_community_image = holder.getView(R.id.rv_community_image);

                rv_community_image.setLayoutManager(new GridLayoutManager(getContext(), 4));

                mImageAdapter = new CommonRecyclerViewAdapter<String>(getContext(), mImageList) {
                    @Override
                    public void convert(CommonRecyclerViewHolder holder, String entity, int position1) {
                        FrameLayout fl_yin = holder.getView(R.id.fl_yin);

                        if (!StringUt.isEmpty(mList.get(position).getIzFee())) {
                            if (mList.get(position).getIzFee().equals("1")) {
                                fl_yin.setVisibility(View.VISIBLE);
                            } else {
                                fl_yin.setVisibility(View.GONE);
                            }
                        } else {
                            fl_yin.setVisibility(View.GONE);
                        }

                        ImageView iv_community_image = holder.getView(R.id.iv_community_image);

                        GlideImgManager.glideLoader(getContext(), entity, iv_community_image, 1);
                    }

                    @Override
                    public int getLayoutViewId(int viewType) {
                        return R.layout.item_community_image_layout;
                    }
                };
                rv_community_image.setAdapter(mImageAdapter);
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_community_type1;
            }
        };

        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (checkStatus.equals("1")) {
                    startActivity(new Intent(getContext(), CommunityDetailsAc.class)
                            .putExtra("id", mList.get(position).getId()));
                }
            }
        });
    }
}
